<?php

namespace App\Admin\Controllers;

use App\Models\OrderGoods;
use App\Models\OrderInvoice;
use App\Models\ProductGoods;
use App\Util\Url;
use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Models\Area;
use App\Models\Order;
use Illuminate\Http\Request;

class OrderInvoiceController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '发票管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new OrderInvoice());
        $grid->disableExport();//禁止导出
        $userid = auth()->user()->id ?? 0;
        $is_work = auth()->user()->is_work;
//        $roleId = Administrator::find($userid)->roles()->first()->pivot->role_id; //获取角色id
//        if ($roleId != 1) {
//            $grid->model()->whereRaw('suppliers_id =' . $userid);//->orderBy('id', 'desc');
//        }
        $where = [];
        if($is_work>0){
            $where [] = ['work_id', '=', $is_work];//供应商ID
        }else{
            $where [] = ['suppliers_id', '=', $userid];//供应商ID
        }
        $grid->model()->where($where);
        $grid->model()->orderByDesc("id");
        $grid->fixColumns(0, 1);//固定列可滑动列表
        $grid->column('id', __('Id'));
        $grid->column('order_id', __('订单号'))->display(function () {
            $sn = (new Order())->where('id', $this->order_id)->value('sn');
            return $sn;
        });
        $grid->column('product_original_price', __('发票金额'))->display(function () {
            $goods_id = (new OrderGoods())->where("order_id",$this->order_id)->value("goods_id");
            $product_original_price = (new ProductGoods())->where("id",$goods_id)->value("product_original_price");
//            $sn = (new Order())->where('id', $this->order_id)->value('sn');
            return $product_original_price;
        });
        // $grid->column('img', __('发票地址'))->display(function () {
        //     if (!empty($this->img)) {
        //         return Url::getHttpHost() . 'upload/' . $this->img;
        //     }
        // })->lightbox(['width' => 150, 'height' => 50]);
        $grid->column('img', '查看发票')->display(function () {
              if (!empty($this->img)) {
                $url = Url::getHttpHost() . 'upload/' . $this->img;
                $text = '点击查看'; // 链接显示的文本
                return "<a href='{$url}' target='_blank'>{$text}</a>";
              }else{
                  return "暂未上传";
              }
        });
        $grid->column('type', __('发票类型'))->display(function () {
            $status = [
                1 => '普通类型',
            ];
            return $status[$this->type] ?? '无';
        });
        $grid->column('content', __('发票内容'));
        $grid->column('head_type', __('抬头类型'))->display(function () {
            $status = [
                1 => '个人',
                2 => '单位',
            ];
            return $status[$this->head_type] ?? '无';
        });
        $grid->column('head_name', __('抬头名称'));
        $grid->column('head_num', __('公司统一信用代码'));
        $grid->column('name', __('收票人名称'));
        $grid->column('phone', __('电话'));
        //省名
        $grid->column('province_id', __('省'))->display(function () {
            $name = (new Area())->where('code', $this->province_id)->value('name');
            return $name;
        });
        //市名
        $grid->column('city_id', __('市'))->display(function () {
            $name = (new Area())->where('code', $this->city_id)->value('name');
            return $name;
        });
        //区名
        $grid->column('district_id', __('县'))->display(function () {
            $name = (new Area())->where('code', $this->district_id)->value('name');
            return $name;
        });
//        $grid->column('img', __('发票连接'))->image(Url::getHttpHost() . 'upload/', 100, 100);
        $grid->column('register_address', __('注册地址'));
        $grid->column('register_phone', __('注册电话'));
        $grid->column('open_bank', __('注册银行'));
        $grid->column('bank_num', __('银行账号'));
        $grid->column('created_at', __('添加时间'));
        $grid->column('updated_at', __('修改时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(OrderInvoice::findOrFail($id));
        $show->panel()->tools(function ($tools) {
            $tools->disableDelete();
        });
        $show->field('id', __('Id'));
        $show->field('order_id', __('订单号'))->as(function () {
            $sn = (new Order())->where([['id', '=', $this->order_id]])->value('sn');
            return $sn;
        });
        $show->field('type', __('发票类型'))->as(function () {
            $status = [
                1 => '普通类型',
            ];
            return $status[$this->type];
        });
        $show->field('content', __('发票内容'));
        $show->field('head_type', __('抬头类型'))->as(function () {
            $status = [
                1 => '个人',
                2 => '单位',
            ];
            return $status[$this->head_type];
        });
        $show->field('head_name', __('抬头名称'));
        $show->field('head_num', __('公司统一信用代码'));
        $show->field('name', __('收票人名称'));
        $show->field('phone', __('电话'));
        $show->field('province_id', __('省'))
            ->unescape()->as(function () {
                $name = (new Area())->where('code', $this->province_id)->value('name');
                return $name;
            });
        $show->field('city_id', __('市'))
            ->unescape()->as(function () {
                $name = (new Area())->where('code', $this->city_id)->value('name');
                return $name;
            });
        $show->field('district_id', __('县'))
            ->unescape()->as(function () {
                $name = (new Area())->where('code', $this->district_id)->value('name');
                return $name;
            });
        $show->field('img', __('发票'))->image(Url::getHttpHost() . 'upload/', 100, 100);
        $show->field('register_address', __('注册地址'));
        $show->field('register_phone', __('注册电话'));
        $show->field('open_bank', __('注册银行'));
        $show->field('bank_num', __('银行账号'));
        $show->field('created_at', __('添加时间'));
        $show->field('updated_at', __('修改时间'));
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = null)
    {
        $form = new Form(new OrderInvoice());
        $form->select('type', __('发票类型'))->options(['1' => "普通类型"])->default(1)->rules('required');
        $form->text('content', __('发票内容'));
        $form->select('head_type', '抬头类型')->options(['1'=>"个人",'2' => "单位"])->default(2)->rules('required');
        $form->text('head_name', __('抬头名称'));
        $form->text('head_num', __('公司统一信用代码'));
        $form->text('name', __('收票人名称'));
        $form->mobile('phone', __('电话'));
        $form->distpicker(['province_id', 'city_id', 'district_id'], '请选择区域');
        $form->file('img', __('上传发票'))->uniqueName()->removable();
        $form->text('register_address', __('注册地址'));
        $form->text('register_phone', __('注册电话'));
        $form->text('open_bank', __('注册银行'));
        $form->text('bank_num', __('银行账号'));
        $form->saving(function (Form $form) {
            $userid = auth()->user()->id ?? 0;
            $is_work = auth()->user()->is_work;
            if($is_work >0){
                 $form->model()->setAttribute('types', 1);
            }else {
               $form->model()->setAttribute('types', 2);
            }
            $form->model()->setAttribute('suppliers_id', $userid);//添加人当前供应商
        });
        return $form;
    }
}
